package com.sumover.Service.impl;

import com.sumover.DAO.*;
import com.sumover.POJO.*;
import com.sumover.Service.GameService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

@Service
public class GameServiceImpl implements GameService {

    @Autowired
    private GameMapper gameMapper;
    @Autowired
    private GameDetailMapper gameDetailMapper;
    @Autowired
    private GameLinkMapper gameLinkMapper;
    @Autowired
    private GameScoreMapper gameScoreMapper;
    @Autowired
    private GameTypeMapper gameTypeMapper;

    @Override
    public List<Game> queryAllGames() {
        List<Game> games = gameMapper.selectAll();
        return games;
    }

    @Override
    public HashMap<Integer, Integer> getGameScoreStatistics(Game game) {
        HashMap<Integer, Integer> scoreStatistics = new HashMap<>();
        List<Integer> allScoreByGame = gameScoreMapper.queryAllScoreByGame(game);
        for (Integer i = 0; i < GameScore.MAX_SCORE; ++i)
            scoreStatistics.put(i, 0);
        for (Integer score : allScoreByGame) {
            if (scoreStatistics.get(score) == null) scoreStatistics.put(score, 0);
            scoreStatistics.put(
                    score,
                    scoreStatistics.get(score) + 1
            );
        }
        return scoreStatistics;
    }

    @Override
    public Double getGameScore(Game game) {
        List<Integer> allScoreByGame = gameScoreMapper.queryAllScoreByGame(game);
        Double sum = 0.0;
        for (Integer s : allScoreByGame) sum += s;
        return sum / allScoreByGame.size();
    }

    @Override
    public Game getGame(Integer gid) {
        Game result = gameMapper.selectByPrimaryKey(gid);
        return result;
    }

    @Override
    public GameDetail getGameDetail(Game game) {
        GameDetail result = gameDetailMapper.selectByPrimaryKey(game.getGid());
        return result;
    }

    @Override
    public List<Game> getGameByTypes(GameType type) {
        List<Game> games = gameMapper.queryGameByType(type);
        return games;
    }

    @Override
    public List<GameType> getAllGameTypes() {
        List<GameType> gameTypes = gameTypeMapper.selectAll();
        return gameTypes;
    }

    @Override
    public List<GameLink> getGameLinks(Game game) {
        List<GameLink> gameLinks = gameLinkMapper.getGameLinks(game);
        return gameLinks;
    }

    @Override
    public Game getGameByName(String gamename) {
        Game gameByName = gameMapper.getGameByName(gamename);
        return gameByName;
    }
}
